package com.example.boot2mybatis.pubsub;

import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CouponService {

    public void sendCoupon(String uname) {
        System.out.println(uname + "随机得到了一张优惠券");
    }

    @Async  // 需要在主程序上使用@EnableAsync开启异步功能
    @Order(2)
    @EventListener
    public void myEventMethod(LoginSuccessEvent event) {
        System.out.println("=====CouponService======收到事件=====");

        UserEntity source = (UserEntity) event.getSource();
        sendCoupon(source.getUname());
    }


}
